import './public-path';
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import {
  BrowserRouter
} from "react-router-dom";
import { ConfigProvider } from 'antd';
import zhCN from 'antd/lib/locale/zh_CN';
import store from './store'
import {setUser} from './store/userReducer'

import 'antd/dist/antd.css'

function render(props) {
  const { container } = props;
  ReactDOM.render(
    <React.StrictMode>
      <ConfigProvider locale={zhCN}>
      <BrowserRouter basename={window.__POWERED_BY_QIANKUN__ ? '/bar' : '/'}>
        <App />
      </BrowserRouter>
      </ConfigProvider>
    </React.StrictMode>, container ? container.querySelector('#root') : document.querySelector('#root'));
}
if (!window.__POWERED_BY_QIANKUN__) {
  render({});
}

export async function bootstrap() {
  console.log('[react17] react app bootstraped');
}

export async function mount(props) {
  console.log('[react17] props from main framework', props);
  let user = null
  props.onGlobalStateChange((state) => {
      // state: 变更后的状态; prev 变更前的状态
      user = state.user
      store.dispatch(setUser(user))
  }, true)
  render(props)
}

export async function unmount(props) {
  const { container } = props;
  ReactDOM.unmountComponentAtNode(container ? container.querySelector('#root') : document.querySelector('#root'));
}